package cn.iocoder.yudao.module.infra.controller.admin.ocr;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.OcrRecognizeByUrlReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.OcrRecognizeReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.OcrRecognizeRespVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.infra.service.ocr.OcrService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;

@Tag(name = "管理后台 - OCR识别")
@RestController
@RequestMapping("/infra/ocr")
@Validated
public class OcrController {

    @Resource
    private OcrService ocrService;

    @Resource
    private FileService fileService;

    @PostMapping("/recognize")
    @Operation(summary = "OCR识别（通过URL）")
    public CommonResult<OcrRecognizeRespVO> recognizeByUrl(@Valid @RequestBody OcrRecognizeByUrlReqVO reqVO) {
        // 根据imageUrl获取文件信息
        FileDO file = fileService.getFileByUrl(reqVO.getImageUrl());
        if (file == null) {
            throw exception(FILE_NOT_EXISTS);
        }
        
        // 调用OCR识别
        return success(ocrService.recognizeGeneralText(reqVO.getConfigId(), file.getId()));
    }

    @PostMapping("/recognize/general")
    @Operation(summary = "通用文本识别")
    @PreAuthorize("@ss.hasPermission('infra:ocr:recognize')")
    public CommonResult<OcrRecognizeRespVO> recognizeGeneral(@Valid @RequestBody OcrRecognizeReqVO reqVO) {
        return success(ocrService.recognizeGeneralText(reqVO.getConfigId(), reqVO.getFileId()));
    }

    @PostMapping("/recognize/idcard")
    @Operation(summary = "身份证识别")
    @PreAuthorize("@ss.hasPermission('infra:ocr:recognize')")
    public CommonResult<OcrRecognizeRespVO> recognizeIdCard(@Valid @RequestBody OcrRecognizeReqVO reqVO) {
        return success(ocrService.recognizeIdCard(reqVO.getConfigId(), reqVO.getFileId()));
    }

    @PostMapping("/recognize/business-license")
    @Operation(summary = "营业执照识别")
    @PreAuthorize("@ss.hasPermission('infra:ocr:recognize')")
    public CommonResult<OcrRecognizeRespVO> recognizeBusinessLicense(@Valid @RequestBody OcrRecognizeReqVO reqVO) {
        return success(ocrService.recognizeBusinessLicense(reqVO.getConfigId(), reqVO.getFileId()));
    }

    @PostMapping("/recognize/bank-card")
    @Operation(summary = "银行卡识别")
    @PreAuthorize("@ss.hasPermission('infra:ocr:recognize')")
    public CommonResult<OcrRecognizeRespVO> recognizeBankCard(@Valid @RequestBody OcrRecognizeReqVO reqVO) {
        return success(ocrService.recognizeBankCard(reqVO.getConfigId(), reqVO.getFileId()));
    }

    @PostMapping("/recognize/driving-license")
    @Operation(summary = "驾驶证识别")
    @PreAuthorize("@ss.hasPermission('infra:ocr:recognize')")
    public CommonResult<OcrRecognizeRespVO> recognizeDrivingLicense(@Valid @RequestBody OcrRecognizeReqVO reqVO) {
        return success(ocrService.recognizeDrivingLicense(reqVO.getConfigId(), reqVO.getFileId()));
    }

    @PostMapping("/recognize/vehicle-license")
    @Operation(summary = "行驶证识别")
    @PreAuthorize("@ss.hasPermission('infra:ocr:recognize')")
    public CommonResult<OcrRecognizeRespVO> recognizeVehicleLicense(@Valid @RequestBody OcrRecognizeReqVO reqVO) {
        return success(ocrService.recognizeVehicleLicense(reqVO.getConfigId(), reqVO.getFileId()));
    }
}